# WISP in a box
# Copyright (C) 2009 Meraka Institute of the CSIR
# All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#


# - configuration -------------------------------------------------------------
RM=rm -rf


# - paths ---------------------------------------------------------------------
UCK_BIN=/usr/bin

UBUNTU_ARCHIVE=../../ubuntu-archive
UBUNTU_RELEASES=../../ubuntu-releases
UBUNTU_HARDY_ISO=$(UBUNTU_RELEASES)/hardy/ubuntu-8.04.2-desktop-i386.iso
UBUNTU_HARDY_PKG=$(UBUNTU_ARCHIVE)/hardy

WIAB_HARDY_PKG=$(BUILD_DIR)/ubuntu-archive/hardy

REMASTER_ISO=$(BUILD_DIR)/remaster-iso
REMASTER_INITRD=$(BUILD_DIR)/remaster-initrd
REMASTER_ROOTFS=$(BUILD_DIR)/remaster-root
REMASTER_SCRIPTS=/tmp/customization-scripts
REMASTER_CACHE=$(BUILD_DIR)/remaster-apt-cache
REMASTER_OUTPUT=$(BUILD_DIR)/remaster-new-files


# - sanity checks -------------------------------------------------------------
ifeq ($(shell [ ! -f "$(UBUNTU_HARDY_ISO)" ] && echo YES), YES)
$(error Could not find iso image for hardy: $(UBUNTU_HARDY_ISO), please download it)
endif

ifeq ($(BUILD_DIR),)
BUILD_DIR=../../build
$(warning Setting build directory to $(BUILD_DIR))
endif

ifeq ($(VERSION),)
VERSION=$(shell date +%Y%m%d%Y%M)
$(warning Setting version to $(VERSION))
endif

REMASTER_LIVECD=wispbox-livecd-$(VERSION).iso


# - build rules ---------------------------------------------------------------

all : prep ubuntu
	#@echo "Restoring .Xauthority"
	#@if test -f $(BUILD_DIR)/.Xauthority.bak ; then mv $(BUILD_DIR)/.Xauthority.bak ~/.Xauthority ; fi

prep : 
	#@echo "Working around uck .Xauthority bug"
	#@if test -f $(BUILD_DIR)/.Xauthority.bak; then rm $(BUILD_DIR)/.Xauthority.bak ; fi
	#@if test -f ~/.Xauthority ; then cp ~/.Xauthority $(BUILD_DIR)/.Xauthority.bak ; fi
	@echo "Building livecd images in $(BUILD_DIR)"
	@if ! test -d $(BUILD_DIR) ; then mkdir $(BUILD_DIR) ; fi


ubuntu : # ubuntu-clean
	@echo "Building livecd for Ubuntu 8.04 LTS - hardy: $(REMASTER_LIVECD)"
	@if ! test -d $(REMASTER_ISO)    ; then make ubuntu-unpack-iso    ; fi
	@if ! test -d $(REMASTER_INITRD) ; then make ubuntu-unpack-initrd ; fi
	@if ! test -d $(REMASTER_ROOTFS) ; then make ubuntu-unpack-rootfs ; fi
	make ubuntu-mutate
	make ubuntu-repack


# - unpack rules
ubuntu-unpack-iso :
	@echo "Unpacking iso: $(UBUNTU_HARDY_ISO)"	
	$(UCK_BIN)/uck-remaster-unpack-iso $(UBUNTU_HARDY_ISO) $(BUILD_DIR)

ubuntu-unpack-initrd :
	@echo "Unpacking initrd to: $(REMASTER_INITRD)"
	$(UCK_BIN)/uck-remaster-unpack-initrd $(BUILD_DIR)

ubuntu-unpack-rootfs :
	@echo "Unpacking rootfs to: $(REMASTER_ROOTFS)"	
	$(UCK_BIN)/uck-remaster-unpack-rootfs $(BUILD_DIR)


# - mutate rules
ubuntu-mutate : ubuntu-mutate-cruft ubuntu-mutate-files ubuntu-mutate-usplash ubuntu-mutate-bootsplash ubuntu-mutate-scripts ubuntu-mutate-artwork
	@echo "Finished mutating livecd root: $(REMASTER_ROOTFS)"

ubuntu-mutate-cruft :
	$(UCK_BIN)/uck-remaster-remove-win32-files $(BUILD_DIR)

ubuntu-mutate-files :
	@echo "Installing files"
	touch $(REMASTER_ROOTFS)/home/killroy-was-here

ubuntu-mutate-usplash :
	# See: https://help.ubuntu.com/community/USplashCustomizationHowto
	# convert -colors 32 logo.png logo4.png
	cd artwork/usplash ; pngtousplash usplash_640_480.png > usplash_640_480.png.c
	cd artwork/usplash ; pngtousplash spinner.png > spinner.png.c
	cd artwork/usplash ; bdftousplash helvB10.bdf > helvB10.bdf.c
	cd artwork/usplash ; gcc -Os -g -I/usr/include/bogl -fPIC -c usplash_640_480.png.c -o usplash_640_480.png.o
	cd artwork/usplash ; gcc -Os -g -I/usr/include/bogl -fPIC -c spinner.png.c -o spinner.png.o
	cd artwork/usplash ; gcc -Os -g -I/usr/include/bogl -fPIC -c helvB10.bdf.c -o helvB10.bdf.o
	cd artwork/usplash ; gcc -Os -g -I/usr/include/bogl -fPIC -c spinner.c -o spinner.o
	cd artwork/usplash ; gcc -shared -Wl,-soname,usplash-artwork.so usplash_640_480.png.o spinner.png.o helvB10.bdf.o spinner.o \
	                            -o usplash-theme-wiab.so 
	cp artwork/usplash/usplash-theme-wiab.so $(REMASTER_ROOTFS)/usr/lib/usplash
	cp artwork/usplash/usplash-theme-wiab.so $(REMASTER_INITRD)/usr/lib/usplash/usplash-artwork.so
	cd artwork/usplash ; rm *.png.c *.bdf.c *.o *.so

ubuntu-mutate-bootsplash :
	cp artwork/bootsplash/isolinux.cfg $(REMASTER_ISO)/isolinux/isolinux.cfg
	cp artwork/bootsplash/isolinux.txt $(REMASTER_ISO)/isolinux/isolinux.txt
	chmod 0444 $(REMASTER_ISO)/isolinux/isolinux.cfg
	chmod 0444 $(REMASTER_ISO)/isolinux/isolinux.txt
	cp artwork/bootsplash/splash.pcx $(REMASTER_ISO)/isolinux/splash.pcx
	cp artwork/bootsplash/splash.rle $(REMASTER_ISO)/isolinux/splash.rle
	chmod 0444 $(REMASTER_ISO)/isolinux/splash.pcx
	chmod 0444 $(REMASTER_ISO)/isolinux/splash.rle
	#pngtopnm artwork/bootsplash/rle.png > artwork/bootsplash/rle.pnm
	# ppmtolss16 "#000000=0" "#ffffff=7" < artwork/bootsplash/rle.pnm > $(REMASTER_ISO)/isolinux/splash.rle
	rm -f artwork/bootsplash/rle.pnm 

ubuntu-mutate-scripts : ubuntu-restore-packages
	@if test -d $(REMASTER_ROOTFS)/$(REMASTER_SCRIPTS) ; then rm -rf $(REMASTER_ROOTFS)/$(REMASTER_SCRIPTS) ; fi
	cp -a customization-scripts $(REMASTER_ROOTFS)/$(REMASTER_SCRIPTS)
	-$(UCK_BIN)/uck-remaster-chroot-rootfs "$(REMASTER_SCRIPTS)/customize" "$(BUILD_DIR)" 
	if -d $(REMASTER_ROOTFS)/tmp/.X11-unix ; then umount -f $(REMASTER_ROOTFS)/tmp/.X11-unix ; fi
	rm -rf $(REMASTER_ROOTFS)/$(REMASTER_SCRIPTS) 
	make ubuntu-cache-packages
	@echo "Cleaning..."
	find $(REMASTER_ROOTFS)/var/cache/apt -name "*.deb" -exec $(RM) '{}' ';'
	#if ! -d $(REMASTER_ROOTFS)/tmp/.X11-unix ; then find $(REMASTER_ROOTFS)/tmp -exec $(RM) '{}' ';' ; fi

ubuntu-mutate-artwork : 
	cp artwork/backgrounds/* $(REMASTER_ROOTFS)/usr/share/backgrounds
	cp artwork/ubuntu-wallpapers.xml $(REMASTER_ROOTFS)/usr/share/gnome-background-properties
	cp artwork/16_ubuntu-wallpapers $(REMASTER_ROOTFS)/usr/share/gconf/defaults


# - utilities
chroot-test :
	cp -u $(REMASTER_CACHE)/archives/mysql-server* $(REMASTER_ROOTFS)/tmp
	@if test -d $(REMASTER_ROOTFS)/$(REMASTER_SCRIPTS) ; then rm -rf $(REMASTER_ROOTFS)/$(REMASTER_SCRIPTS) ; fi
	cp -a customization-scripts $(REMASTER_ROOTFS)/$(REMASTER_SCRIPTS)
	-$(UCK_BIN)/uck-remaster-chroot-rootfs $(REMASTER_SCRIPTS)/test $(BUILD_DIR)
	if test -d $(REMASTER_ROOTFS)/tmp/.X11-unix ; then umount -f $(REMASTER_ROOTFS)/tmp/.X11-unix ; fi
	rm -rf $(REMASTER_ROOTFS)/$(REMASTER_SCRIPTS)


# - package caching
ubuntu-restore-packages : .dummy
	@if ! test -d $(REMASTER_CACHE)/archives/partial ; then mkdir -p $(REMASTER_CACHE)/archives/partial ; fi
	@if test -d $(UBUNTU_HARDY_PKG); then cp -u $(UBUNTU_HARDY_PKG)/*.deb $(REMASTER_CACHE)/archives ; fi
	@if test -d $(WIAB_HARDY_PKG); then cp -u $(WIAB_HARDY_PKG)/*.deb $(REMASTER_CACHE)/archives ; fi # config packages

ubuntu-cache-packages :
	@if ! test -d $(UBUNTU_HARDY_PKG) ; then mkdir $(UBUNTU_HARDY_PKG) ; fi
	rm -rf $(UBUNTU_HARDY_PKG)/wispinabox*.deb 
	cp -u $(REMASTER_CACHE)/archives/*.deb $(UBUNTU_HARDY_PKG)
	cp $(REMASTER_CACHE)/archives/wispinabox*.deb $(UBUNTU_HARDY_PKG)


# - repack rules
ubuntu-repack :
	@echo "Repacking livecd"
	$(UCK_BIN)/uck-remaster-pack-initrd $(BUILD_DIR)
	$(UCK_BIN)/uck-remaster-pack-rootfs -c $(BUILD_DIR)
	$(UCK_BIN)/uck-remaster-pack-iso "$(REMASTER_LIVECD)" $(BUILD_DIR)
	@echo "Successfully generated livecd: $(REMASTER_OUTPUT)/$(REMASTER_LIVECD)"


# - utilities
ubuntu-clean :
	@echo "Cleaning build directory"
	$(UCK_BIN)/uck-remaster-clean $(BUILD_DIR)


.dummy :
